package com.msl.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;
import com.msl.MslApp;
import com.msl.R;
import com.msl.adapter.ProductAdapter;
import com.msl.base.BaseActivity;
import com.msl.bean.AbDcodeBean;
import com.msl.bean.AbStateBean;
import com.msl.scan.BroadcastConfig;
import com.msl.tool.ToastUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;

//条码删除
public class DJTZ3codeActivity extends BaseActivity {
    @Bind(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tvs)
    TextView tvs;
    @Bind(R.id.etcode)
    EditText etcode;
    @Bind(R.id.bt)
    Button bt;
    @Bind(R.id.ll)
    LinearLayout ll;
    @Bind(R.id.tv)
    TextView tv;
    @Bind(R.id.iv1)
    ImageView iv1;
    @Bind(R.id.tv1)
    TextView tv1;
    @Bind(R.id.iv2)
    ImageView iv2;
    @Bind(R.id.tv2)
    TextView tv2;
    @Bind(R.id.sws)
    Switch sws;
    @Bind(R.id.rl)
    RelativeLayout rl;
    @Bind(R.id.list)
    ListView list;
    private String tag = "DJTZ3codeActivity";
    private AbStateBean abStateBean;
    private List<AbStateBean.Data> listdata = new ArrayList<AbStateBean.Data>();
    private ProductAdapter sa;
    private boolean flag = false;
    private AbDcodeBean abDcodeBean = new AbDcodeBean();
    private AbDcodeBean abDcodeBeantemp = new AbDcodeBean();
    private List<AbDcodeBean.DataBean> abDcodeBean1;
    private int index = 0;
    private boolean deletefalse = true;

    protected BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
             if (intent.getAction().equals(BroadcastConfig.CUSTOM_NAME)) {
                if(deletefalse)
                {
                    deletecode(MslApp.url + "?action=DeleteCode&cid=" + MslApp.sSharedPreferences.getString(MslApp.cidk, "")
                            + "&userID=" + MslApp.sSharedPreferences.getInt(MslApp.userID, 0)
                            + "&storeID=" + MslApp.sSharedPreferences.getInt(MslApp.storeID, 0)
                            + "&billcode=" + abDcodeBean.getData().get(index).getBillcode()
                            + "&code=" + intent.getStringExtra("scannerdata")
                            + "&codeType=" + "1"
                    );
                }
                else {
                    String str = "";
                    if (!flag) {
                        str = "1";
                    } else {
                        str = "2";
                    }
                    String code = abDcodeBean1.get(index).getBillcode() + "," + abDcodeBean1.get(index).getProductID() + "," +
                            str + "," + intent.getStringExtra("scannerdata") + "," + abDcodeBean1.get(index).getSubBillCode() + "," + abDcodeBean1.get(index).getManualNums();
                    MslApp.listcode.add(code);
                }
            } else if (intent.getAction().equals("netcsucc")) {
                tvs.setText("网络状态:正常");
            } else if (intent.getAction().equals("netcfail")) {
                tvs.setText("网络状态:异常");
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_djtzcode);
        ButterKnife.bind(this);
        tvTitle.setText("单据调整");
        initBroadcastReciever();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BroadcastConfig.CUSTOM_NAME);
        intentFilter.addAction("netcsucc");
        intentFilter.addAction("netcfail");
        registerReceiver(mBroadcastReceiver, intentFilter);
        abDcodeBean = (AbDcodeBean) getIntent().getSerializableExtra(MslApp.codedea);
        abDcodeBean1 = abDcodeBean.getData();
        sa = new ProductAdapter(this, abDcodeBean1);
        list.setAdapter(sa);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                sa.setSelectedPosition(position);
                sa.notifyDataSetInvalidated();
                index = position;
            }
        });
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(mBroadcastReceiver);
        super.onDestroy();
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @OnClick({R.id.iv_title_left, R.id.iv1, R.id.tv1, R.id.iv2, R.id.tv2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.iv1:
                flag = false;
                iv1.setImageResource(R.mipmap.u107);
                iv2.setImageResource(R.mipmap.u108);
                break;
            case R.id.tv1:
                flag = false;
                iv1.setImageResource(R.mipmap.u107);
                iv2.setImageResource(R.mipmap.u108);
                break;
            case R.id.iv2:
                flag = true;
                iv1.setImageResource(R.mipmap.u108);
                iv2.setImageResource(R.mipmap.u107);
                break;
            case R.id.tv2:
                flag = true;
                iv1.setImageResource(R.mipmap.u108);
                iv2.setImageResource(R.mipmap.u107);
                break;
        }
    }

    void deletecode(String url) {
        try {
            Request request = new Request.Builder()
                    .url(url)
                    .build();
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                    waitDialog.dismiss();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (response.isSuccessful()) {//回调的方法执行在子线程。
                        result = response.body().string();
                        Log.e(tag, result);
                        waitDialog.dismiss();
                        handler.sendEmptyMessage(0);
                    } else {
                        Log.e(tag, "获取数据失败了" + response.code());
                        waitDialog.dismiss();
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            waitDialog.dismiss();
        }
    }


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    abDcodeBeantemp = gson.fromJson(result, AbDcodeBean.class);
                    if (abDcodeBeantemp.getErrcode() == 0) {
                        abDcodeBean = abDcodeBeantemp;
                        abDcodeBean1.clear();
                        abDcodeBean1.addAll(abDcodeBean.getData());
                        sa.notifyDataSetChanged();
                    }
                    else
                    {
                        ToastUtils.showSingleToast(DJTZ3codeActivity.this,abDcodeBeantemp.getErrmsg());
                        MediaPlayer mediaPlayer01;
                        mediaPlayer01 = MediaPlayer.create(DJTZ3codeActivity.this, R.raw.cf);
                        mediaPlayer01.start();
                    }
                    break;
                default:
                    break;

            }
        }
    };

}
